﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace SharpSoft.WPF.Controls
{
    public class FieldEdit : ContentControl
    {
        public FieldEdit()
        {
            this.Focusable = false;
            this.Height = 25;
            this.Margin = new Thickness(5);
            this.DataContextChanged += FieldEdit_DataContextChanged;
            var ct = new ControlTemplate();
            FrameworkElementFactory fe1 = new FrameworkElementFactory(typeof(DockPanel));
            fe1.SetValue(DockPanel.LastChildFillProperty, true);
            FrameworkElementFactory fe2 = new FrameworkElementFactory(typeof(ContentPresenter));
            fe2.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
            //fe2.SetValue(ContentPresenter.TextAlignmentProperty, TextAlignment.Right);
            fe2.SetValue(ContentPresenter.MarginProperty, new Thickness(0, 0, 4, 0));
            Binding b1 = new Binding(HeaderProperty.Name);
            b1.Source = this;
            Binding b2 = new Binding(HeaderWidthProperty.Name);
            b2.Source = this;
            fe2.SetBinding(ContentPresenter.ContentProperty, b1);
            fe2.SetBinding(ContentPresenter.WidthProperty, b2);

            FrameworkElementFactory fe3 = new FrameworkElementFactory(typeof(ContentPresenter));
            Binding b3 = new Binding(ContentProperty.Name);
            b3.Source = this;

            fe3.SetBinding(ContentPresenter.ContentProperty, b3);
            fe3.SetValue(ContentPresenter.ContentSourceProperty, "Content");
            fe1.AppendChild(fe2);
            fe1.AppendChild(fe3);


            ct.VisualTree = fe1;
            this.Template = ct;
        }

        private void FieldEdit_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.Content is FrameworkElement fe)
            {
                fe.DataContext = this.DataContext;
            }
        }



        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }
         
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(FieldEdit), new PropertyMetadata("标题"));




        public double HeaderWidth
        {
            get { return (double)GetValue(HeaderWidthProperty); }
            set { SetValue(HeaderWidthProperty, value); }
        }

        public static readonly DependencyProperty HeaderWidthProperty =
            DependencyProperty.Register("HeaderWidth", typeof(double), typeof(FieldEdit), new PropertyMetadata(100d));

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            if (newContent is Control ctrl)
            {
                ctrl.VerticalContentAlignment = VerticalAlignment.Center;
                ctrl.Background = Brushes.White;
            }
            if (newContent is DatePicker date)
            {

            }
        }


    }
}
